package org.changyou.dbtool.schemaexport.common;

import java.io.File;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.changyou.dbtool.schemaexport.mysql.MysqlSchemaExportImp;
import org.changyou.dbtool.schemaexport.oracle.OracleSchemaExportImp;
import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class CommonExport
{
  public static void main(String[] args)
  {
    String datasourcefile = args[0];
    ApplicationContext context = new FileSystemXmlApplicationContext(
      datasourcefile);
    String[] names = context
      .getBeanNamesForType(StandardXAPoolDataSource.class);
    if (names != null) {
      String path = System.getProperty("user.dir");
      System.out.println("user.dir=" + path);
      MysqlSchemaExportImp mysql_tool = new MysqlSchemaExportImp();
      OracleSchemaExportImp oracle_tool = new OracleSchemaExportImp();
      label303: for (String name : names) {
        StandardXAPoolDataSource datasource = (StandardXAPoolDataSource)context
          .getBean(name);
        try {
          String dbtype = datasource.getConnection().getMetaData().getDatabaseProductName();
          if ("oracle".equalsIgnoreCase(dbtype)) {
            file_path = path + File.separatorChar + name + "_oracle.sql";
            System.out.println("start export to:" + file_path);
            oracle_tool.export(datasource, file_path);
            System.out.println("done"); break label303: }
          if (!("mysql".equalsIgnoreCase(dbtype))) break label303;
          String file_path = path + File.separatorChar + name + "_mysql.sql";
          System.out.println("start export to:" + file_path);
          mysql_tool.export(datasource, file_path);
          System.out.println("done");
        }
        catch (SQLException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
}